package jp.co.ricoh.melody.views.fragments;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import jp.co.ricoh.melody.R;
import jp.co.ricoh.melody.models.entities.SoundData;
import jp.co.ricoh.melody.presenters.ISoundListDialogFragmentPresenter;
import jp.co.ricoh.melody.presenters.SoundListDialogFragmentPresenter;
import jp.co.ricoh.melody.utils.CUtil;
import jp.co.ricoh.melody.views.adapters.SoundRecycleView;

/**
 * Copyright (C) 2016 RICOH Co.,LTD.
 * All rights reserved.
 * <p/>
 * This fragment is used to display sound picker.
 */
public class SoundListDialogFragment extends DialogFragment implements View.OnClickListener {

    /**
     * This view is used to display list sounds.
     */
    private SoundRecycleView mRecyclerView;
    /**
     * This button is used to select sound for melody and dismiss dialog.
     */
    private Button mOkBtn;
    /**
     * This button is used to dismiss dialog.
     */
    private Button mCancelBtn;

    private Button mGetSDCardSoundBtn;
    /**
     * Presenter of this view.
     */
    private ISoundListDialogFragmentPresenter mISoundListDialogFragmentPresenter;

    private SoundData mSoundData;

    private static final String ARG_SOUND_DATA = "ARG_SOUND_DATA";

    public SoundListDialogFragment() {
    }

    public interface OnSoundDialogDismissListener {
        void onDismiss(SoundData soundData);
    }

    public static SoundListDialogFragment newInstance(SoundData soundData) {
        SoundListDialogFragment soundListDialogFragment = new SoundListDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ARG_SOUND_DATA, soundData);
        soundListDialogFragment.setArguments(bundle);
        return soundListDialogFragment;
    }

    public void setOnDismissListener(OnSoundDialogDismissListener onSoundDialogDismissListener) {
        mISoundListDialogFragmentPresenter = new SoundListDialogFragmentPresenter();
        mISoundListDialogFragmentPresenter.onCreate(getActivity(), onSoundDialogDismissListener);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dialog_sound_picker, container, false);
        initView(view);

        return view;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        // Set dialog fragment without title.
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        mSoundData = (SoundData) getArguments().getSerializable(ARG_SOUND_DATA);
        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        // Set size for dialog.
        if (dialog != null) {
            dialog.getWindow().setLayout(getResources().getDimensionPixelOffset(R.dimen.sound_pciker_dialog_width),
                    getResources().getDimensionPixelOffset(R.dimen.sound_pciker_dialog_height));
        }
    }

    /**
     * This method is used to initialize view.
     *
     * @param view
     */
    private void initView(View view) {
        mRecyclerView = (SoundRecycleView) view.findViewById(R.id.fragment_dialog_sound_picker_recyclerview);
        mRecyclerView.onCreate(getActivity(), mSoundData);
        mOkBtn = (Button) view.findViewById(R.id.fragment_dialog_sound_btn_ok);
        mOkBtn.setOnClickListener(this);
        mCancelBtn = (Button) view.findViewById(R.id.fragment_dialog_sound_btn_cancel);
        mCancelBtn.setOnClickListener(this);
        mGetSDCardSoundBtn = (Button) view.findViewById(R.id.fragment_dialog_sound_btn_get_sdcard_sound);
        mGetSDCardSoundBtn.setOnClickListener(this);

    }

    private void clickImageViewSound() {

        mRecyclerView.onDestroyView();
        if (!Environment.getExternalStorageState()
                .equals(Environment.MEDIA_MOUNTED)) {
            // SDカードは未挿入の状態
            CUtil.showToast(getActivity(),getString(R.string.txt_sdcard_find_fail));
            return;
        }
        final SDCardSoundListDialogFragment sdCardSoundListDialogFragment = SDCardSoundListDialogFragment.newInstance(mSoundData);
              sdCardSoundListDialogFragment.setOnDismissListener(new SoundListDialogFragment.OnSoundDialogDismissListener() {
            @Override
            public void onDismiss(SoundData soundData) {

                if (soundData != null) {
                    if(mRecyclerView.addSoundItem(soundData)) {
                        mSoundData = soundData;
                    } else {
                        //Toast.makeText(getActivity(), R.string.txt_sdcard_sound_fail, Toast.LENGTH_LONG).show();
                        CUtil.showToast(getActivity(),getString(R.string.txt_sdcard_sound_fail));
                    }
                }
            }
        });
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            sdCardSoundListDialogFragment.show(getFragmentManager(), null);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fragment_dialog_sound_btn_ok:
                mISoundListDialogFragmentPresenter.onCompletedSelectSound(mRecyclerView.getSelectedItem());
                dismissAllowingStateLoss();
                break;
            case R.id.fragment_dialog_sound_btn_cancel:
                dismissAllowingStateLoss();
                break;
            case R.id.fragment_dialog_sound_btn_get_sdcard_sound:
                clickImageViewSound();
                break;
            default:
                break;
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {

        mRecyclerView.onDestroyView();
        super.onDismiss(dialog);
    }

}
